package my.soft.aiteacher.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class TakeTimeAspect {
    //统计请求的处理时间
    ThreadLocal<Long> startTime = new ThreadLocal<>();
    ThreadLocal<Long> endTime = new ThreadLocal<>();
    @Before("execution(* my.soft.aiteacher.teacher.controller..*.*(..))")
    public void doBefore(JoinPoint joinPoint){
        // 获取方法的名称
        String methodName = joinPoint.getSignature().getName();
        // 获取方法入参
        Object[] param = joinPoint.getArgs();
        StringBuilder sb = new StringBuilder();
        for (Object o : param) {
            sb.append(o).append(";");
        }
        log.info("<------------------进入{} 方法,参数为: {}---------------->", methodName,sb);
        startTime.set(System.currentTimeMillis());
    }
    @AfterReturning(returning = "ret", pointcut = "execution(* my.soft.aiteacher.teacher.controller..*.*(..))")
    public void doAfterReturning(Object ret) {
        //处理完请求后，返回内容
        endTime.set(System.currentTimeMillis());
        //获取返回值
        log.info("方法的返回值 : " + ret);
        //获取方法执行时间
        log.info("方法执行时间:" + ((endTime.get() - startTime.get()) + "ms"));
    }

}